package com.xtgg.main;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.history.HistorySumActivity;
import com.xtgg.history.ShareActivity;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.ImageLoaderHelper;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.PreferencesUtils;
import com.xtgg.util.TLog;
import com.xtgg.util.ToastUtil;
import com.xtgg.util.Utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchCancelActivity extends BaseActivity implements OnClickListener {
	JSONObject mInfo;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_cancel);
		
		mInfo = JSON.parseObject(getIntent().getStringExtra("baby"));

		((TextView) findViewById(R.id.center)).setText("寻人信息");
		((Button) findViewById(R.id.right)).setText("转发");
		((Button) findViewById(R.id.right)).setTextColor(Color
				.parseColor("#EE7C1C"));
		
		Date date = Utils.ConverToDate(mInfo.getString("birth"), "yyyy-MM-dd");
		date = new Date(System.currentTimeMillis() - date.getTime());
		
		if(date.getYear() == 0)
		{
			((TextView)findViewById(R.id.age)).setText(date.getMonth() + "个月");
		}
		else
		{
			((TextView)findViewById(R.id.age)).setText(date.getYear() + "岁");
		}
		
		((TextView)findViewById(R.id.name)).setText(mInfo.getString("childname"));
		((TextView)findViewById(R.id.value0)).setText(mInfo.getString("misstime"));
		((TextView)findViewById(R.id.value1)).setText(mInfo.getString("missplace"));
		((TextView)findViewById(R.id.value2)).setText(mInfo.getString("description"));
		((TextView)findViewById(R.id.value3)).setText(mInfo.getString("remark"));

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.action0).setOnClickListener(this);
		findViewById(R.id.login).setOnClickListener(this);

		ImageLoaderHelper.displayImage(R.drawable.img_default,
				(ImageView) findViewById(R.id.img),
				PreferencesUtils.getString(this, "headurl"), 5);
		
		if(mInfo.getInteger("status") == 1) findViewById(R.id.login).setVisibility(View.INVISIBLE);
	}

	@Override
	public void onClick(View arg0) {
		int viewId = arg0.getId();
		switch (viewId) {
		case R.id.action0:
		{
			Intent intent = new Intent(this,SearchDetailActivity.class);
			intent.putExtra("baby", getIntent().getStringExtra("baby"));
			startActivity(intent);
		}
			break;

		case R.id.left:
			finish();
			break;

		case R.id.right:
			showQuery();
			break;
			
		case R.id.login:
			sendHttpRequest();
			break;
		}
	}
	

	private void sendHttpRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				TLog.d(TAG, "response=" + response);
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					ToastUtil.showToastWithAlertPic("寻人信息撤回成功");
					finish();
				} 
				else if (ruser.getState().equals("2")) 
				{showExitDialog();} 
				else {
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};

		RequestParams params = new RequestParams();
		params.put("act", "operateInfo");
		params.put("userid", PreferencesUtils.getString(this, "userid"));
		params.put("type", "1");
		params.put("infoid", mInfo.getString("infoid"));
		
		MgqRestClient.get(Constants.INFOMGT, params, loginHandler);
	}
	
	Dialog mDialog = null;
	void showQuery() {
		OnClickListener listen = new OnClickListener()
		{
			@Override
			public void onClick(View arg0) {
				int viewId = arg0.getId();
				switch (viewId) {
				case R.id.login:
					mDialog.dismiss();
					break;
					
				case R.id.action0:
				{
					HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
					
					Intent intent = new Intent(SearchCancelActivity.this,ShareActivity.class);
					intent.putExtra("type", 0);
					intent.putExtra("content", "小兔乖乖寻人");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;

				case R.id.action1:
				{
					HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
					
					Intent intent = new Intent(SearchCancelActivity.this,ShareActivity.class);
					intent.putExtra("type", 1);
					intent.putExtra("content", "小兔乖乖足迹寻人");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;

				case R.id.action2:
				{
					HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
					
					Intent intent = new Intent(SearchCancelActivity.this,ShareActivity.class);
					intent.putExtra("type", 2);
					intent.putExtra("content", "小兔乖乖足迹寻人");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;

				case R.id.action3:
				{
					HistorySumActivity.mScreenShot = convertViewToBitmap(findViewById(R.id.screen));
					
					Intent intent = new Intent(SearchCancelActivity.this,ShareActivity.class);
					intent.putExtra("type", 3);
					intent.putExtra("content", "小兔乖乖足迹寻人");
					startActivity(intent);
				}
					mDialog.dismiss();
					break;
				}
			}
		};
		
		mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_share);
		mDialog.findViewById(R.id.action0).setOnClickListener(listen);
		mDialog.findViewById(R.id.action1).setOnClickListener(listen);
		mDialog.findViewById(R.id.action2).setOnClickListener(listen);
		mDialog.findViewById(R.id.action3).setOnClickListener(listen);
		mDialog.findViewById(R.id.login).setOnClickListener(listen);
		mDialog.show();
	}
	
	 public Bitmap convertViewToBitmap(View view) {
		 WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		 int width = wm.getDefaultDisplay().getWidth();
		 int height = wm.getDefaultDisplay().getHeight();
		 
		 Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);  
		 view.draw(new Canvas(b));
		 return b;
	 }
}
